<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" session="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.*, loginbean.*" %>
<html>
	<head>
		<% Vector<ChangeSvcBean> newSvcOrder = new Vector<ChangeSvcBean>(); %>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Customize a Work Flow</title>
		<script type="text/javascript">
			var counter = 0;
	
			function moreFields(selectobj, var2) 
			{
				counter++;
				var newFields = document.getElementById('readroot').cloneNode(true);
				newFields.id = '';
				newFields.style.display = 'block';
				var newField = newFields.childNodes;
				for (var i = 0; i < newField.length; i++) 
				{
					var theName = newField[i].name
					if (theName)
					{
						newField[i].name = theName + counter;
						newField[i].value = selectobj.selectedIndex +"-"+ var2;
					}
				}
				var insertHere = document.getElementById('writeroot');
				insertHere.parentNode.insertBefore(newFields,insertHere);
			}
	
			window.onload = moreFields;
		</script>		
	</head>
	<body>
		<form action="workflowcontroller" method=post>
			Select a Work Flow to Customize: 
			<% out.println( WorkFlow.getAllWorkFlows( Integer.parseInt( session.getAttribute("tenantid").toString() ) ) ); %>
			<input type="hidden" name="viewcmd" value="B" />
			<input type="submit" value="Retrieve Work Flow" />
		</form>
		<%
			Vector<String> serviceNames = (Vector)session.getValue("serviceNames");
			Vector<Integer> serviceOrder = (Vector)session.getValue("serviceOrder");
			if(serviceNames != null && serviceNames.size() != 0)
			{
		%>

				<table border=1>
					<tr>
						<th>Web Service Name</th>
						<th>Current Operation Order</th>
						<th>New Operation Order</th>
						<th>Delete Web Service</th>
					</tr>
		<%
					for(int i = 0; i < serviceNames.size(); i ++)
					{
						out.println("<tr>");
						out.println("<td>"+ serviceNames.elementAt(i) +"</td>");
						out.println("<td>"+ serviceOrder.elementAt(i) +"</td>");
						out.println("<td>");
						out.println("<SELECT name='svcOrder' onChange=\"moreFields(this,'"+ serviceNames.elementAt(i) +"')\">");
						int highestSvcNum = WorkFlow.getMaxSvcNum(Integer.parseInt( session.getAttribute("tenantid").toString()), (String)session.getAttribute("workFlowName"));
						out.println("<option></option>");
						for(int j = 1; j < highestSvcNum + 1 ; j ++)
						{
							out.println("<option>"+ j + "</option>");
						}
						out.println("</SELECT>");
						out.println("</td>");
						out.println("<td><form action=\"workflowcontroller\" method=post>");
						out.println("<input type=\"hidden\" name=\"viewcmd\" value=\"D\" />");
						out.println("<input type=\"hidden\" name=\"del\" value=\""+ serviceOrder.elementAt(i) +"\" />");
						out.println("<input type=\"submit\" value=\"Delete\" />");
						out.println("</td>");
						out.println("</tr>");
					}
		%>
				</table>
				<div id="readroot" style="display: none">
					<input type="hidden" name="test" value="" />
				</div>
				<form action="workflowcontroller" method=post>
					<input type="hidden" name="viewcmd" value="C" />
					<span id="writeroot"></span>
					<input type="submit" value="Submit New Work Flow" />
				</form>
				<form action="workflowcontroller" method=post>
					<input type="hidden" name="viewcmd" value="E" />
					Select a new Web Service: 
					<% out.println( WorkFlow.getAllWebServices( Integer.parseInt( session.getAttribute("tenantid").toString() ) ) ); %>
					<input type="submit" value="Add This New Service to the Work Flow" />
				</form>				
		<%				
			}
		%>				 
	</body>
</html>